package cqrtplm.aop;


import cn.hutool.extra.spring.SpringUtil;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.common.model.dto.search.SearchConditionDTO;
import com.hustcad.plm.pdm.common.model.vo.search.ResultEntityVO;
import com.hustcad.plm.pdm.common.service.search.TyplmCommonSearchService;
import com.hustcad.plm.pdm.doc.vo.DocumentVO;
import com.hustcad.plm.pdm.iba.service.impl.TyplmAttributeServiceImpl;
import com.hustcad.plm.pdm.middle.partbom.service.TyplmPdmPartService;
import com.hustcad.plm.pdm.type.mapper.TyplmTypeMapper;
import com.ty.basic.entity.part.PartReferenceLinkDO;
import com.ty.basic.entity.type.TypeDO;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.utils.BatchQueryHelper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Aspect
@Component
@Slf4j
public class RTDocumentBindPartAOP {

    @Resource
    private TyplmCommonSearchService typlmCommonSearchService;

    @Resource
    private TyplmPdmPartService typlmPdmPartService;

    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;

    @Resource
    private TyplmTypeMapper typlmTypeMapper;
    @Around(value = "execution(* com.hustcad.plm.pdm.doc.controller.TyplmDocumentController.insertDoc(..))")
    public ResponseResult<DocumentVO> DocumentBindPart(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        Object proceed = point.proceed();
        ResponseResult<DocumentVO> documentVO = (ResponseResult<DocumentVO>) proceed;
        String oid = documentVO.getData().getDocOid();
        String otype = documentVO.getData().getOtype();
        BigInteger oidBigInteger = BigInteger.valueOf(Long.parseLong(oid));
        //零部件 文档获取iba属性
        TyplmAttributeServiceImpl bean = SpringUtil.getBean(TyplmAttributeServiceImpl.class);
        Map<String, List<String>> ibaAttributeMap = bean.getIbaAttributeMap(oidBigInteger, otype);
        log.info("物料IBA属性={}", ibaAttributeMap.toString());
        if (!ObjectUtils.isEmpty(ibaAttributeMap)) {
            if (!ObjectUtils.isEmpty(ibaAttributeMap.get("关联零部件"))) {
                if (!ObjectUtils.isEmpty(ibaAttributeMap.get("关联零部件").get(0))) {
                    String str = ibaAttributeMap.get("关联零部件").get(0);
                    //对象字符串用 ；分割
                    String[] split = str.split(";");//[ewe,yhtyhy,eeerv,jhyh ,frfr,juj ,tgty,juju]
                    for (String s : split) {
                        String identification = s.split(",")[0];
                        SearchConditionDTO searchConditionDTO = new SearchConditionDTO();
                        searchConditionDTO.setKeyword(identification);
                        PageInfo<ResultEntityVO> resultEntityVOPageInfo = typlmCommonSearchService.queryByConditionForPage(
                                searchConditionDTO);
                        List<ResultEntityVO> list = resultEntityVOPageInfo.getList();
                        ResultEntityVO resultEntityVO = list.get(0);
                        String partOtype = resultEntityVO.getOtype();
                        BigInteger partOid = resultEntityVO.getOid();

                        List<PartReferenceLinkDO> objLst = new ArrayList();

                        PartReferenceLinkDO pDO = new PartReferenceLinkDO();
                        pDO.setAoid(partOid);
                        pDO.setAotype(partOtype);
                        pDO.setBoid(new BigInteger(documentVO.getData().getMasteredoid()));
                        pDO.setBotype(documentVO.getData().getMasteredotype());
                        pDO.setOid(this.snowflakeIdComponent.getInstance().nextId());
                        pDO.setTypeotype("ty.inteplm.part.CTyPartReferenceLink");
                        Set<String> typeOtypes = new HashSet<>();
                        typeOtypes.add("ty.inteplm.part.CTyPartReferenceLink");
                        List<TypeDO> typeDOS = BatchQueryHelper.queryExampleColumnIn(typeOtypes, TypeDO.class, "logicalidentifier", this.typlmTypeMapper);
                        Map<String, BigInteger> typeOidMap = typeDOS.stream().collect(Collectors.toMap(TypeDO::getLogicalidentifier, TypeDO::getOid));
                        pDO.setTypeoid(typeOidMap.get(pDO.getTypeotype()));

                        objLst.add(pDO);
                        this.typlmPdmPartService.batchInsertPartReferenceLink(objLst, true);
                    }
                }
            }
        }
        return documentVO;
    }

}
